﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sentry.Tool
{
    public class Log
    {
        /// <summary>
        /// 锁对象
        /// </summary>
        private object _lockHelper = new object();

        public void WriteInfo(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【INFO】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-info", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteInfo(string dir, string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【INFO】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, dir, "log-info", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteDebug(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【DEBUG】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-debug", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteDebug(string dir, string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【DEBUG】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, dir, "log-debug", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteWarning(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【WARNING】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-warning", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteWarning(string dir, string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【WARNING】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, dir, "log-warning", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteError(string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【ERROR】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, "log-error", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteError(string dir, string content)
        {
            DateTime dt = DateTime.Now;
            string info = "【ERROR】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 " + content;
            WriteLog(info, dir, "log-error", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteException(Exception ex)
        {
            DateTime dt = DateTime.Now;
            string info = "【EXCEPTION】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 异常信息：" + ex.Message + "\r\n堆栈信息：" + ex.StackTrace;
            WriteLog(info, "log-exception", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteException(string dir, Exception ex)
        {
            DateTime dt = DateTime.Now;
            string info = "【EXCEPTION】『" + dt.ToString("yyyy-MM-dd HH:mm:ss") + "』 异常信息：" + ex.Message + "\r\n堆栈信息：" + ex.StackTrace;
            WriteLog(info, dir, "log-exception", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteOnly(string content)
        {
            DateTime dt = DateTime.Now;
            string info = content;
            WriteLog(info, "log-only", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        public void WriteOnly(string dir, string content)
        {
            DateTime dt = DateTime.Now;
            string info = content;
            WriteLog(info, dir, "log-only", dt.ToString("yyyy-MM-dd") + ".txt");
        }

        /// <summary>
        /// 自定义写日志
        /// </summary>
        /// <param name="fileFullName">文件名</param>
        /// <param name="value">日志内容</param>
        public void WriteLogCustom(string fileFullName, string value)
        {
            try
            {
                lock (_lockHelper)
                {
                    StreamWriter sw = new StreamWriter(fileFullName, true, Encoding.UTF8);
                    sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "：" + value);
                    //sw.WriteLine("\r\n");
                    sw.Close();
                }

                Console.ForegroundColor = ConsoleColor.DarkGray;
                Console.Write(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+ "：");
                Console.WriteLine(value);
                Console.ResetColor();
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="strContent">日志内容</param>
        /// <param name="dir">目录</param>
        /// <param name="filename">文件名称</param>
        private void WriteLog(string strContent, string dir, string filename)
        {
            try
            {
                lock (_lockHelper)
                {
                    string logFilePath = GetPath("logs");
                    if (!Directory.Exists(logFilePath))
                        Directory.CreateDirectory(logFilePath);
                    logFilePath = Path.Combine(logFilePath, dir);
                    if (!Directory.Exists(logFilePath))
                        Directory.CreateDirectory(logFilePath);
                    string file = Path.Combine(logFilePath, filename);
                    StreamWriter sw = new StreamWriter(file, true, Encoding.UTF8);
                    sw.WriteLine(strContent);
                    //sw.WriteLine("\r\n");
                    sw.Close();
                }
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        /// <param name="strContent">日志内容</param>
        /// <param name="customDir">自定义目录</param>
        /// <param name="typeDir">类型目录</param>
        /// <param name="filename">文件名称</param>
        private void WriteLog(string strContent, string customDir, string typeDir, string filename)
        {
            try
            {
                lock (_lockHelper)
                {
                    string logFilePath = GetPath("logs");
                    if (!Directory.Exists(logFilePath))
                        Directory.CreateDirectory(logFilePath);

                    logFilePath = Path.Combine(logFilePath, customDir);
                    if (!Directory.Exists(logFilePath))
                        Directory.CreateDirectory(logFilePath);

                    logFilePath = Path.Combine(logFilePath, typeDir);
                    if (!Directory.Exists(logFilePath))
                        Directory.CreateDirectory(logFilePath);
                    string file = Path.Combine(logFilePath, filename);
                    StreamWriter sw = new StreamWriter(file, true, Encoding.UTF8);
                    sw.WriteLine(strContent);
                    //sw.WriteLine("\r\n");
                    sw.Close();
                }
            }
            catch (Exception)
            {

            }
        }

        private const string blank = "      ";
        public void WriteInfoAndConsole(string content)
        {
            WriteInfo(content);

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("信息");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();
        }

        public void WriteInfoAndConsole(string dir, string content)
        {
            WriteInfo(dir, content);

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.Write("信息");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();
        }

        public void WriteDebugAndConsole(string content)
        {
            WriteDebug(content);

            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.Write("调试");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();
        }

        public void WriteDebugAndConsole(string dir, string content)
        {
            WriteDebug(dir, content);

            Console.ForegroundColor = ConsoleColor.DarkGray;
            Console.Write("调试");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();
        }

        public void WriteWarningAndConsole(string content)
        {
            WriteWarning(content);

            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write("警告");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();

        }

        public void WriteWarningAndConsole(string dir, string content)
        {
            WriteWarning(dir, content);

            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.Write("警告");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();
        }

        public void WriteErrorAndConsole(string content)
        {
            WriteError(content);

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write("错误");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();
        }

        public void WriteErrorAndConsole(string dir, string content)
        {
            WriteError(dir, content);

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.Write("错误");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + content);
            Console.ResetColor();
        }

        public void WriteExceptionAndConsole(Exception ex)
        {
            WriteException(ex);

            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write("异常");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + ex.Message + Environment.NewLine + ex.StackTrace);
            Console.ResetColor();
        }

        public void WriteExceptionAndConsole(string dir, Exception ex)
        {
            WriteException(dir, ex);

            Console.ForegroundColor = ConsoleColor.DarkRed;
            Console.Write("异常");
            Console.ResetColor();
            Console.WriteLine("：" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            Console.WriteLine(blank + ex.Message + Environment.NewLine + ex.StackTrace);
            Console.ResetColor();
        }



        public void WriteOnlyAndConsole(string content)
        {
            WriteOnly(content);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(content);
            Console.ResetColor();
        }



        public void WriteOnlyAndConsole(string dir, string content)
        {
            WriteOnly(dir, content);

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine(content);
            Console.ResetColor();
        }

        private string GetPath(string strPath)
        {
            string rootDir = AppContext.BaseDirectory;

            string dir = string.Empty;
            if (!string.IsNullOrEmpty(strPath))
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.Substring(strPath.IndexOf('\\', 1)).TrimStart('\\');
                }
                dir = Path.Combine(rootDir, strPath);
            }
            else
            {
                dir = rootDir;
            }

            return dir;
        }
    }
}
